package cn.com.cetccst.uicomponents.binding.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import cn.com.cetccst.uicomponents.R;


public class MarkImageView extends FrameLayout {

    private RoundImageView image;
    private ImageView webAppFlag;
    private TextView badge;

    public MarkImageView(Context context) {
        super(context);
    }

    public MarkImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        int defaultRadius = 0;

        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.MarkImageView);
        int imageSize = arr.getDimensionPixelSize(R.styleable.MarkImageView_image_size, 0);
        int flagSize = arr.getDimensionPixelSize(R.styleable.MarkImageView_flag_size, 0);
        int radius = arr.getDimensionPixelOffset(R.styleable.MarkImageView_m_radius, defaultRadius);
        arr.recycle();

        View view = LayoutInflater.from(context).inflate(R.layout.layout_markimageview, this);
        image = view.findViewById(R.id.image);
        webAppFlag = view.findViewById(R.id.flag);
        badge = view.findViewById(R.id.badge);

        if (imageSize > 0) {
            image.getLayoutParams().width = imageSize;
            image.getLayoutParams().height = imageSize;
        }
        if (flagSize > 0) {
            webAppFlag.getLayoutParams().width = flagSize;
            webAppFlag.getLayoutParams().height = flagSize;
        }
        image.setRadius(radius);
    }

    public void setBadge(int badgeNumber) {
        badge.setVisibility(badgeNumber == 0 ? INVISIBLE : VISIBLE);

        if (badgeNumber < 100) {
            badge.setText(String.valueOf(badgeNumber));
        } else {
            badge.setText("99+");
        }

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public void showFlag(String type) {
        webAppFlag.setVisibility(INVISIBLE);
        if (TextUtils.isEmpty(type)) {
            return;
        }
//        switch (type) {
//            case AppType.TYPE_WEB:
//                webAppFlag.setVisibility(VISIBLE);
//                webAppFlag.setImageResource(R.drawable.ic_web_flag);
//                break;
//            case AppType.TYPE_MINIAPP:
//                webAppFlag.setVisibility(VISIBLE);
//                webAppFlag.setImageResource(R.drawable.ic_mini_flag);
//                break;
//        }
    }

    public RoundImageView getImage() {
        return image;
    }

    public ImageView getWebAppFlag() {
        return webAppFlag;
    }

    public TextView getBadge() {
        return badge;
    }

    @BindingAdapter(value = {"loadUrl", "image", "imageDrawable"}, requireAll = false)
    public static void loadUrl(MarkImageView markImageView, String url, int drawableId, Drawable drawable) {
        if (TextUtils.isEmpty(url) && drawableId > 0) {
            markImageView.getImage().setImageResource(drawableId);
            return;
        } else if (!TextUtils.isEmpty(url)) {
            Glide.with(markImageView.getContext())
                    .load(url)
                    .placeholder(R.drawable.uicomponents_ic_placeholder)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(markImageView.getImage());
            return;
        }
        if (drawable != null) {
            markImageView.getImage().setImageDrawable(drawable);
        } else {
            markImageView.getImage().setImageResource(R.drawable.uicomponents_ic_placeholder);
        }
    }

    @BindingAdapter(value = {"typeFlag"})
    public static void typeFlag(MarkImageView markImageView, String typeFlag) {
        markImageView.showFlag(typeFlag);
    }

    @BindingAdapter(value = {"grayState"})
    public static void grayState(MarkImageView markImageView, boolean isGrayState) {
        markImageView.getImage().setColorFilter(null);
        markImageView.getBadge().setTextColor(markImageView.getContext().getResources().getColor(R.color.uicomponents_white));
        markImageView.getWebAppFlag().setColorFilter(null);

        if (isGrayState) {
            ColorMatrix matrix = new ColorMatrix();
            matrix.setSaturation(0);
            ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
            markImageView.getImage().setColorFilter(filter);
            markImageView.getBadge().setTextColor(markImageView.getContext().getResources().getColor(R.color.uicomponents_gray));
            markImageView.getWebAppFlag().setColorFilter(filter);
            markImageView.setBadge(0);
        }
    }

    @BindingAdapter(value = {"badgeNumber"})
    public static void badgeNumber(MarkImageView markImageView, int badgeNumber) {
        markImageView.setBadge(badgeNumber);
        markImageView.getBadge().setVisibility(badgeNumber > 0 ? VISIBLE : INVISIBLE);
    }

}
